$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/jxcourseschedule/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true},
            {label: '课程', name: 'courseName', index: 'course_id', width: 80},
            {label: '实验室', name: 'laboratoryName', index: 'laboratory_id', width: 80},
            {label: '授课老师', name: 'teacherName', index: 'teacher_id', width: 80},
            {label: '开始时间', name: 'begainTime', index: 'begain_time', width: 80},
            {label: '课程批次', name: 'batch', index: 'batch', width: 80},
            {label: '结束时间', name: 'endTime', index: 'end_time', width: 80},
            {label: '授课日期', name: 'courseDay', index: 'course_day', width: 80},
            {label: '当前使用率', name: 'usageRate', index: 'usage_rate', width: 80, formatter: function (cellvalue, options, rowObject) {
                return cellvalue*100+"%";
                }},
            {
                label: '操作',
                name: 'status',
                index: 'status',
                width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    var laboratoryId = rowObject.laboratoryId;
                    var scheduleId = rowObject.id;

                    if (cellvalue == "0") {
                        return "<a class='btn btn-default' onclick='showDevice(" + scheduleId + "," + laboratoryId +  "," + cellvalue + ")' ><i class='fa fa-desktop' style='font-size: 20px'></i>&nbsp;分配</a>";
                    }
                    return "<a class='btn btn-default' onclick='showDevice(" + scheduleId + "," + laboratoryId + "," + cellvalue + ")' ><i class='fa fa-link' style='font-size: 20px'></i>&nbsp;查看</a>";
                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        shrinkToFit:true,
        autoScroll: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "scroll"});
        }
    });

    $('#begainTime').datetimepicker({
        language: 'zh-CN',
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1,
        format: "yyyy-mm-dd hh:ii:ss",
        startDate: "2000-01-01 00:00:00",// 开始时间
    });

    $('#endTime').datetimepicker({
        language: 'zh-CN',
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1,
        format: "yyyy-mm-dd hh:ii:ss",
        startDate: "2000-01-01 00:00:00",// 开始时间
    });

    $('#courseDay').datetimepicker({
        language: 'zh-CN',
        todayBtn: 1,
        autoclose: 1,
        minView: 2,
        format: "yyyy-mm-dd",
        startDate: "2000-01-01 00:00:00",// 开始时间
    });

    vm.getALLCourse();   //  拿到所有的课程
    vm.getAllTeacher();   //  拿到所有的课程
    vm.getAllLaboratory();   //  拿到所有的课程

});

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            courseDay: null,
            laboratoryId: null,
            teacherName: null,
            courseName: null,
            batch: null
        },
        showList: true,
        title: null,
        allCourse: [{"name": "请选择", "id": ""}],
        allTeacher: [{"userName": "请选择", "userId": ""}],
        allLaboratory: [{"name": "请选择", "id": ""}],
        jxCourseSchedule: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.jxCourseSchedule = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            console.log($("#beginTime").val());
            vm.jxCourseSchedule.begainTime = $("#begainTime").val();
            vm.jxCourseSchedule.endTime = $("#endTime").val();
            vm.jxCourseSchedule.courseDay = $("#courseDay").val();
            vm.jxCourseSchedule.status=0;
            console.log(vm.jxCourseSchedule);
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function () {
                var url = vm.jxCourseSchedule.id == null ? "sys/jxcourseschedule/save" : "sys/jxcourseschedule/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.jxCourseSchedule),
                    success: function (r) {
                        if (r.code === 0) {
                            layer.msg("操作成功", {icon: 1});
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        } else {
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                if (!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/jxcourseschedule/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            } else {
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function () {
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/jxcourseschedule/info/" + id, function (r) {
                vm.jxCourseSchedule = r.jxCourseSchedule;
                $("#begainTime").val(r.jxCourseSchedule.begainTime);
                $("#endTime").val(r.jxCourseSchedule.endTime);
            });
        },
        reload: function (event) {
            vm.showList = true;
            vm.q.courseDay = $("#courseDay").val();
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page,
                postData: {
                    'courseDay': vm.q.courseDay,
                    'laboratoryId': vm.q.laboratoryId,
                    'teacherName': vm.q.teacherName,
                    'courseName': vm.q.courseName,
                    'batch': vm.q.batch
                }
            }).trigger("reloadGrid");
        },
        getALLCourse: function () { //  所有课程
            $.get(baseURL + "sys/jxcourse/allList", function (r) {
                console.log(r);
                vm.allCourse = r.list;
            });
        }, getAllTeacher: function () {  //  拿到所有的教师
            $.get(baseURL + "/sys/user/allList", function (r) {
                console.log(r);
                vm.allTeacher = r.list;
            });
        }, getAllLaboratory: function () {  // 拿到所有实验室
            $.get(baseURL + "sys/jxlaboratory/allList", function (r) {
                console.log(r);
                vm.allLaboratory = r.list;
            });
        }
    }
});

function showDevice(id, laboratoryId, v) {
    window.location.href = "computyDevices.html?id=" + id + "&&laboratoryId=" + laboratoryId + "&&view=" + v ;
}